export const size = 9;

// 内置对象
const getParamsObj = (childrenKey, v) => {
	let params = "";
	let i = 1;
	for (let key in v) {
		if (v[key] !== null) {
			if (i >= 2) {
				params += `${childrenKey}[${key}]=${v[key]}`
			} else if (v[key].constructor === Object) {
				params += getParamsObj(`${childrenKey}[${key}]`, v[key])
			} else {
				params += `&${childrenKey}[${key}]=${v[key]}`
			}
		}
	}
	return params
}

// 参数获取
export const getParam = (v) => {
	let param = "?";
	let i = 0;
	// 循环遍历对象
	for (let key in v) {
		// 记录属性值
		i++;
		if (v[key] !== null) {
			// 属性有多个
			if (i >= 2) {
				// 是否为对象
				if (v[key].constructor === Object) {
					param += getParamsObj(key, v[key]);
				} else {
					param += `&${key}=${v[key]}`;
				}
			} else {
				param += `${key}=${v[key]}`
			}
		}
	};
	return param
}

// 测试参数
const query = {
	page: 1,
	size: 2,
	filters: {
		name: "123",
		state: null,
		cs: {
			a: null,
			b: 2,
			c: {
				d: 1
			}
		}
	}
};

// 页面栈,页面栈一旦超出10，清空所有页面，再跳转
export const uniNavigateTo = (url, events, success) => {
	const pages = getCurrentPages();
	if (pages.length >= 10) {
		uni.reLaunch({
			url,
			success
		})
	} else {
		uni.navigateTo({
			url,
			events,
			success
		});
	}
}


// 获取日期
export const getDate = (v) => {
	const date = new Date(Number(v) * 1000);
	// 年
	const year = date.getFullYear();
	// 月
	const month = date.getMonth() + 1 > 10 ? date.getMonth() + 1 : '0' + date.getMonth();
	// 日
	const dates = date.getDate() > 10 ? date.getDate() : '0' + date.getDate();
	// 时
	const hours = date.getHours() > 10 ? date.getHours() : '0' + date.getHours();
	// 分
	const mount = date.getMinutes() > 10 ? date.getMinutes() : '0' + date.getMinutes();
	// 秒
	const seconds = date.getSeconds() > 10 ? date.getSeconds() : '0' + date.getSeconds();
	return `${year}-${month}-${dates} ${hours}:${mount}:${seconds}`;
}
